//
//  HDSelectMonthCollectionHeader.m
//  MirkoLibTest
//
//  Created by Mirko on 2017/2/14.
//  Copyright © 2017年 Mirko. All rights reserved.
//

#import "HDSelectMonthCollectionHeader.h"

const static int kLeftGap = 16;
const static int kRightGap = 16;
const static int kTopGap = 10;
const static int kBottomGap = 10;

@interface HDSelectMonthCollectionHeader ()

@property (strong, nonatomic) UILabel *note;
@property (strong, nonatomic) NSString *noteStr;
@property (strong, nonatomic) NSMutableAttributedString *attrString;
@property (assign, nonatomic) CGRect textRelRct;

@end

@implementation HDSelectMonthCollectionHeader


-(instancetype) initWithNote:(NSString *) text
{
    self.textRelRct = [self getRelRect:text];
    CGRect viewRct = CGRectMake(0, 0, ScreenWidth(), self.textRelRct.size.height);
    
    self = [self initWithFrame:viewRct];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:232/255.0 alpha:1];
        self.noteStr = text;
        [self setNoteString];
    }
    return self;
}

-(instancetype) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        
        
        [self addSubview:self.note];
        
//        [self.note mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.edges.mas_equalTo(UIEdgeInsetsMake(16, 10, 16, 10));
//        }];
    }
    return self;
}

-(void) setNoteString
{
    self.note.attributedText = self.attrString;
}

-(CGRect) getRelRect:(NSString *) text
{
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineSpacing = 6;
    UIFont *font = [UIFont systemFontOfSize:12];
    NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f
                          };
    
    self.attrString = [[NSMutableAttributedString alloc] initWithString:text attributes:dic];
    CGFloat width = ScreenWidth() - kLeftGap - kRightGap;
    CGSize attSize = [self.attrString boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
    
    CGRect result = CGRectMake(0, 0, width, attSize.height + kTopGap + kBottomGap);
    
    
    self.note = [[UILabel alloc] initWithFrame:CGRectMake(kLeftGap, kTopGap, width, attSize.height)];
    self.note.font = [UIFont systemFontOfSize:12];
    self.note.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
    self.note.backgroundColor = [UIColor clearColor];
    self.note.numberOfLines = 0;
    
    
    return result;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
